package com.auto.reg.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.auto.reg.util.CmdUtil;
import com.auto.reg.util.FileUtil;
import com.auto.reg.util.RegConstant;

public class TesseractOCR {
	
    public static String crackVerifyCode() {

    	String verifyCode = "";
    	BufferedReader readResult = null;
    	String OCR_HOME = "D:\\Program Files\\Tesseract-OCR";
    	File verifyImg = FileUtil.getVerifyCodeImg(RegConstant.VERIFY_IMG_PREFIX);
    	File ocrResult = new File(OCR_HOME + "\\" + RegConstant.OCR_RESULT + ".txt");

    	try {
        	if (!ocrResult.exists()) {
        		ocrResult.createNewFile();
        	}
        	if (null == verifyImg) {
        	    System.out.println("Cannot get verify image");
        	    return "";
        	}

        	String result = CmdUtil.executeCommand("tesseract " + "\"" + verifyImg.getAbsolutePath() + "\" \"" + OCR_HOME + "\\" + RegConstant.OCR_RESULT + "\" -l test");
        	
        	if (!result.equals("")) {
        		readResult = new BufferedReader(new FileReader(ocrResult));
        		verifyCode = readResult.readLine();
        	}
    		
    		System.out.println(verifyCode);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (readResult != null) {
					readResult.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			FileUtil.deleteDirectiory(RegConstant.IE_LOCAL_DIR);
			CmdUtil.cleanIECache();
		}
    	return verifyCode;
    }

    public static String crackVerifyCode(File verifyImg) {

    	String verifyCode = "";
    	BufferedReader readResult = null;
    	String OCR_HOME = System.getenv(RegConstant.OCR_ENV);
    	System.out.println("home:" + OCR_HOME);
    	File ocrResult = new File(OCR_HOME + "\\" + RegConstant.OCR_RESULT + ".txt");

    	try {
        	if (!ocrResult.exists()) {
        		ocrResult.createNewFile();
        	}

        	String result = CmdUtil.executeCommand("tesseract " + "\"" + verifyImg.getAbsolutePath() + "\" \"" + OCR_HOME + "\\" + RegConstant.OCR_RESULT + "\" -l test");
        	
        	if (!result.equals("")) {
        		readResult = new BufferedReader(new FileReader(ocrResult));
        		verifyCode = readResult.readLine();
        	}
    		
    		System.out.println(verifyCode);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (readResult != null) {
					readResult.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			FileUtil.deleteDirectiory(RegConstant.IE_LOCAL_DIR);
			CmdUtil.cleanIECache();
		}
    	return verifyCodeFilter(verifyCode);
    }
    
    private static String verifyCodeFilter(String verifyCode) {
    	
    	if (verifyCode == null) {
    		return "";
    	}
    	String regEx = "[^a-zA-Z0-9]";
    	Pattern p = Pattern.compile(regEx);     
        Matcher m = p.matcher(verifyCode);     
        return m.replaceAll("").trim();     
    }

    public static void main(String[] args) {
//    	crackVerifyCode();
//    	CmdUtil.cleanIECache();
    	File test = new File("D:/vc_img/");
    	for (File f:test.listFiles()) {
    		String verifyCode = TesseractOCR.crackVerifyCode(f);
    		System.out.println("name:" + f.getName() + " code:" + verifyCode);
    	}
	}
}
